#!/usr/bin/env groovy

node {
    //获取模块列表
    String sonModelTask = 'task perfmaProject(){rootProject.subprojects {println(\\"\\\\$\\${it.projectDir.toString().replace(getRootDir().path.toString(), \\"\\")}\\\\$\\")}}'
    //检出工程
    stage("check out") {
        def verification = ["appName-${appName}", "appKey-${appKey}", "version-${version}", "commitId-${commitId}"]
        for(str in verification){
            def param = str.split('-')
            if("${coverage}" == "1" && param.length == 1)
        	{
        		throw new Exception("参数${param[0]}不能为空")
        	}
        }
        sh '''
            mkdir -p ./${BUILD_NUMBER}/project;
            mkdir -p ./${BUILD_NUMBER}/config;
            
            cd ./${BUILD_NUMBER}/project;
            git clone https://gitee.com/yanglixue/test-arrow-code.git ./;
            git checkout ${commitId}
            cd ../config;
            git clone https://gitee.com/yanglixue/test-arrow-config.git ./;
        '''
    }
    
    stage("injection") {
        if(coverage != "1") return
        sh "echo \"\\n${sonModelTask} \" >> ./${BUILD_NUMBER}/project/build.gradle"
        //模块列表
        def modelList = sh(script: '''cd ./${BUILD_NUMBER}/project/;./gradlew perfmaProject | grep -o "\\$.*\\$" | sed "s/\\\\$//g"''', returnStdout:true).trim().split('\n')
        for (model in modelList){
            def tag = model.trim();
            if(fileExists("./${BUILD_NUMBER}/project${tag}/build.gradle")){
                // 拷贝文件
                sh "echo \"\\napply from: rootProject.file('jacoco_ec.gradle') \" >> ./${BUILD_NUMBER}/project${tag}/build.gradle"
            }
        }
        
        sh "sed -i 's/\${appKey}/${appKey}/g' ./${BUILD_NUMBER}/config/jacoco_ec.gradle"
        sh "sed -i 's/\${version}/${version}/g' ./${BUILD_NUMBER}/config/jacoco_ec.gradle"
        sh "sed -i 's/\${serviceAddress}/http:\\/\\/${serviceAddress}/g' ./${BUILD_NUMBER}/config/jacoco_ec.gradle"
        sh "cp  ./${BUILD_NUMBER}/config/jacoco_ec.gradle ./${BUILD_NUMBER}/project/jacoco_ec.gradle"
        sh "cp  ./${BUILD_NUMBER}/config/jacocoUtil.aar ./${BUILD_NUMBER}/project/jacocoUtil.aar"
        
    }
    
    stage("build"){
        // 构建打包
        sh '''
            cd ./${BUILD_NUMBER}/project/;
            chmod 777 gradlew;
            ./gradlew assembleRelease;
        '''
    }
    
    stage("move"){
        String baseUrl = "build/intermediates/javac/release/classes"
        sh '''
            if [ ! -d "/var/ossfs/${appKey}/${version}/classes" ];then
              mkdir -p /var/ossfs/${appKey}/${version}/classes
            fi
            cp ./${BUILD_NUMBER}/project/app/build/outputs/apk/release/app-release.apk /var/ossfs/${appKey}/${version}/app-release.apk;
        '''
        // def modelList = sh(script: '''cat ./${BUILD_NUMBER}/project/settings.gradle | grep -o ":[^\\(\\'\\|\\"\\)]*" | sed "s/://g"''', returnStdout:true).trim().split('\n')
        def modelList = sh(script: '''cd ./${BUILD_NUMBER}/project/;./gradlew perfmaProject | grep -o "\\$.*\\$" | sed "s/\\\\$//g"''', returnStdout:true).trim().split('\n')
        for (model in modelList){
            def tag = model.trim();
            sh """
                if [ ! -d "/var/ossfs/${appKey}/${version}/classes${tag}" ];then
                    mkdir -p /var/ossfs/${appKey}/${version}/classes${tag}
                fi
                cp -r ./${BUILD_NUMBER}/project${tag}/${baseUrl} /var/ossfs/${appKey}/${version}/classes${tag}
                if [ -d "./${BUILD_NUMBER}/project${tag}/build/tmp/kotlin-classes" ];then
                    if [ ! -d "/var/ossfs/${appKey}/${version}/classes${tag}/kotlin-classes" ];then
                        mkdir -p /var/ossfs/${appKey}/${version}/classes${tag}/kotlin-classes
                    fi
                    cp -r ./${BUILD_NUMBER}/project${tag}/build/tmp/kotlin-classes /var/ossfs/${appKey}/${version}/classes${tag}/kotlin-classes;
                fi
            """
        }
    }
    
    stage("upload"){
        sh '''
            curl -H "Content-Type: application/json" -d \
                '{\
                    "key":"'${appKey}'",\
                    "name":"'${appName}'",\
                    "system":"android",\
                    "version":"'${version}'",\
                    "commitId": "'${commitId}'",\
                    "gitUrl": "https://gitee.com/yanglixue/test-arrow-code.git"\
                }' -i http://${serviceAddress}/api/app
        '''
    }
}
